package com.banksoft.cps.controller.admin;

import com.banksoft.cps.bean.CompanyUser;
import com.banksoft.cps.bean.common.Page;
import com.banksoft.cps.bean.common.SuccessTip;
import com.banksoft.cps.bean.query.CompanyUserQuery;
import com.banksoft.cps.controller.ControllerConstants;
import com.banksoft.cps.service.CompanyUserService;
import com.banksoft.cps.util.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by rongzhao.zhu on 2017/12/21.
 */
@Controller
@RequestMapping("/admin/companyUser/")
public class AdminCompanyUserController extends ControllerConstants {

    @Autowired
    CompanyUserService companyUserService;

    @RequestMapping(value = "list", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String list(CompanyUserQuery query){
        List<CompanyUser> list =  companyUserService.list(query);
        return toJSONString(createDataTip(list));
    }
    @RequestMapping(value = "count", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String count(CompanyUserQuery query){
        long count =  companyUserService.count(query);
        long pageCount = PageUtils.getPage(count,query.getSize());
        return toJSONString(createDataTip(pageCount));
    }
    @RequestMapping(value = "find",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String find(String id){
        CompanyUser companyUser = companyUserService.findById(id);
        return toJSONString(createDataTip(companyUser));
    }

    @RequestMapping(value = "findByCompanyId",produces = "application/json;charset=utf-8")
    @ResponseBody
    public String findByCompanyId(String companyId){
        CompanyUser companyUser = companyUserService.findByCompanyId(companyId);
        if(companyUser ==null){
            companyUser = new CompanyUser();
            companyUser.setCompanyId(companyId);
        }
        return toJSONString(createDataTip(companyUser));
    }

    @RequestMapping(value = "save", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String save(CompanyUser user, HttpServletRequest request){
        try {
            companyUserService.save(user);
            return toJSONString(SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return toExceptionString(e);
        }
    }


}
